import {
	AUTHORIZATION
} from '@/api/config.js'
import {
	$config
} from '@/api/request/index.js'
import * as login from '@/api/login.js'
import * as cache from './cache.js'

export function mergeObjSamePro(obj1, obj2) {
	const res = {}
	Object.keys(obj2).map(key => {
		if (key in obj1) {
			res[key] = obj2[key]
		}
	})
	return res
}

export function showToast(param){
	if(!param.icon){
		param.icon='none'
	}
	uni.showToast({
		title: param.msg,
		icon:param.icon,
		duration: param.duration || 1500
	})
}

/**
 * 判断本地登录状态
 */
export function isLogin() {
	const token = cache.get(AUTHORIZATION);
	if (token) {
		return true
	} else {
		return false
	}
}

/**
 * 判断本地授权状态
 */
export function isAuth() {
	const userInfo = cache.get(cache.key.userInfo);
	if (userInfo && userInfo.avatar) {
		return true
	} else {
		go('/pages/my/auth')
		return false
	}
}

/**
 * 页面跳转
 * @param {Object} url
 */
export function go(url) {
	uni.navigateTo({
		url: url
	})
}

export function formatDate(date, fmt) {
	try {
		if (!(date instanceof Date)) {
			try {
				date = date.replace(/-/g, "/")
				date = new Date(date)
			} catch (e) {
				return '-'
			}
		}
		if (fmt === undefined || fmt === '') {
			fmt = 'yyyy-MM-dd hh:mm'
		}
		if (/(y+)/.test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
		}
		const o = {
			'M+': date.getMonth() + 1,
			'd+': date.getDate(),
			'h+': date.getHours(),
			'm+': date.getMinutes(),
			's+': date.getSeconds()
		}
		for (const k in o) {
			if (new RegExp(`(${k})`).test(fmt)) {
				const str = o[k] + ''
				fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : ('00' + str).substr(str.length))
			}
		}
		return fmt
	} catch (e) {
		console.log(e)
		return '-'
	}
}

/**
 * 获取图片完整路径
 */
export function getImg(img, def) {
	if (!img) {
		return def
	}
	if (img instanceof Object) {
		return $config.fileDomain + img.path
	}
	return $config.fileDomain + img
}

/**
 * 日期转换为xx前
 * @param {Object} dateTime
 */
export function formatMsgTime(dateTime) {
	if (!(dateTime instanceof Date)) {
		try {
			dateTime = dateTime.replace(/-/g, "/")
			dateTime = new Date(dateTime)
		} catch (e) {
			return '-'
		}
	}

	var timespan = dateTime.getTime()

	var year = dateTime.getFullYear();
	var month = dateTime.getMonth() + 1;
	var day = dateTime.getDate();
	var hour = dateTime.getHours();
	var minute = dateTime.getMinutes();
	var second = dateTime.getSeconds();
	var now = new Date();
	var now_new = now.getTime()

	var milliseconds = 0;
	var timeSpanStr;

	milliseconds = now_new - timespan;

	if (milliseconds <= 1000 * 60 * 1) {
		timeSpanStr = '刚刚';
	} else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
		timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
	} else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
		timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
	} else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
		timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
	} else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == now.getFullYear()) {
		if (month < 10) {
			month = "0" + month
		}
		if(day<10){
			day = "0" + day
		}
		if(hour<10){
			hour = "0" + hour
		}
		if(minute<10){
			minute = "0" + minute
		}
		timeSpanStr = month + '-' + day + ' ' + hour + ':' + minute;
	} else {
		if (month < 10) {
			month = "0" + month
		}
		if(day<10){
			day = "0" + day
		}
		if(hour<10){
			hour = "0" + hour
		}
		if(minute<10){
			minute = "0" + minute
		}
		timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
	}
	return timeSpanStr;
};
